package com.sprd.simple.util;

import org.json.JSONArray;

import com.sprd.simple.launcher.Launcher;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Build;
import android.telecom.PhoneAccount;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.sprd.simple.launcher.R;
import com.sprd.simple.model.TextSpeech;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;

import java.util.List;

public class KeyCodeEventUtil {
    public static String[] spData = new String[2];
    public static String[] mStrArray = new String[2];
    public static final String TAG = "KeyCodeEventUtil";
    public static final String DATABASE = "FamilyNumber";

    public static boolean isLauncherNeedUseKeycode(int keyCode) {
        Log.i(TAG, "isLauncherNeedUseKeycode = " + keyCode);
        switch (keyCode) {
            case KeyEvent.KEYCODE_0:
            case KeyEvent.KEYCODE_1:
            case KeyEvent.KEYCODE_2:
            case KeyEvent.KEYCODE_3:
            case KeyEvent.KEYCODE_4:
            case KeyEvent.KEYCODE_5:
            case KeyEvent.KEYCODE_6:
            case KeyEvent.KEYCODE_7:
            case KeyEvent.KEYCODE_8:
            case KeyEvent.KEYCODE_9:
            case KeyEvent.KEYCODE_STAR:
            case KeyEvent.KEYCODE_POUND:
            case KeyEvent.KEYCODE_DPAD_UP:
            case KeyEvent.KEYCODE_DPAD_DOWN:
            case KeyEvent.KEYCODE_DPAD_LEFT:
            case KeyEvent.KEYCODE_DPAD_RIGHT:
            case KeyEvent.KEYCODE_DPAD_CENTER:
            case KeyEvent.KEYCODE_MENU:
            case KeyEvent.KEYCODE_BACK:
            case KeyEvent.KEYCODE_CALL:
            case KeyEvent.KEYCODE_F1:
            case KeyEvent.KEYCODE_F2:
            case KeyEvent.KEYCODE_F3:
            case KeyEvent.KEYCODE_F4:
                return true;
            default:
                return false;
        }
    }

    private static boolean isLauncher(Context context) {
        ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> list = mActivityManager.getRunningTasks(1);
        if (!list.isEmpty() && list.get(0) != null
                && list.get(0).topActivity != null) {
            String className = list.get(0).topActivity.getClassName();
            if (className.equals("com.sprd.simple.launcher.Launcher")) {
                return true;
            }
            Log.w("sprocomm", "className = " + className);
        }
        return false;
    }

    public static boolean pressKeyEventForMainActivity(Context context, int keyCode, KeyEvent event) {
        Intent intent = new Intent();
        if (isLauncher(context)) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_0:
                    context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
                            .fromParts(PhoneAccount.SCHEME_TEL, "0", null)));
                    break;

                case KeyEvent.KEYCODE_1:
                    context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
                            .fromParts(PhoneAccount.SCHEME_TEL, "1", null)));
                    break;

                case KeyEvent.KEYCODE_2:
                    context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
                            .fromParts(PhoneAccount.SCHEME_TEL, "2", null)));
                    break;

                case KeyEvent.KEYCODE_3:
                    context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
                            .fromParts(PhoneAccount.SCHEME_TEL, "3", null)));
                    break;

                case KeyEvent.KEYCODE_4:
                    context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
                            .fromParts(PhoneAccount.SCHEME_TEL, "4", null)));
                    break;

                case KeyEvent.KEYCODE_5:
                    context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
                            .fromParts(PhoneAccount.SCHEME_TEL, "5", null)));
                    break;

                case KeyEvent.KEYCODE_6:
                    context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
                            .fromParts(PhoneAccount.SCHEME_TEL, "6", null)));
                    break;

                case KeyEvent.KEYCODE_7:
                    context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
                            .fromParts(PhoneAccount.SCHEME_TEL, "7", null)));
                    break;

                case KeyEvent.KEYCODE_8:
                    context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
                            .fromParts(PhoneAccount.SCHEME_TEL, "8", null)));
                    break;

                case KeyEvent.KEYCODE_9:
                    context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
                            .fromParts(PhoneAccount.SCHEME_TEL, "9", null)));
                    break;

                case KeyEvent.KEYCODE_STAR:
                    context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
                            .fromParts(PhoneAccount.SCHEME_TEL, "*", null)));
                    break;

                case KeyEvent.KEYCODE_POUND:
                    context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
                            .fromParts(PhoneAccount.SCHEME_TEL, "#", null)));
                    break;

                case KeyEvent.KEYCODE_F1:
                    intent.setClassName(context.getString(R.string.wechat_package), context.getString(R.string.wechat_activity));
                    AppIntentUtil.intentSetFlag(intent);
                    context.startActivity(intent);
                    break;

                case KeyEvent.KEYCODE_CALL:
                    Log.d("hxf", "KeyEvent.KEYCODE_CALL  start");
                    intent.setClassName(context.getString(R.string.phone_package), context.getString(R.string.phone_activity));
                    AppIntentUtil.intentSetFlag(intent);
                    context.startActivity(intent);
                    break;
            }
        }
        return true;
    }

    public static boolean longPressKeyEventForMainActivity(Context context,
                                                           int keyCode, KeyEvent event) {
        Log.i(TAG, "longPressKeyEvent keyCode = " + keyCode);
        return true;
    }

    /**
     * get the data from SharedPreference
     *
     * @param context
     * @param position
     * @param arrayLength
     * @return
     */
    private static String[] getFamilyNumber(Context context, int position,
                                            int arrayLength) {
        try {
            SharedPreferences sharedPreferences = context.getSharedPreferences(
                    DATABASE, Context.MODE_PRIVATE);
            mStrArray = new String[arrayLength];
            JSONArray jsonArray = new JSONArray(sharedPreferences.getString(
                    position + "", ""));
            for (int i = 0; i < jsonArray.length(); i++) {
                mStrArray[i] = jsonArray.getString(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mStrArray;
    }

    public static boolean callFamilyItem(Context context, int position) {
        SharedPreferences sp = context.getSharedPreferences(DATABASE, Context.MODE_PRIVATE);
        if (sp.contains(String.valueOf(position)) == true) {
            getFamilyNumber(context, position, spData.length);
            Intent intent = new Intent(Intent.ACTION_CALL);
            Log.d(TAG, "strArray[1] = " + mStrArray[1]);
            Uri data = Uri.parse("tel:" + mStrArray[1]);
            intent.setData(data);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (context.checkSelfPermission(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                    context.startActivity(intent);
                } else {
                    return false;
                }
            } else {
                context.startActivity(intent);
            }

            return true;
        } else {
            return false;
        }
    }

}
